﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using POSPlus.PrintManager.Core; 
using System.Windows.Forms;
using Util;

namespace POSPlus.PrintManager
{
	public class PrintManagerModel : INotifyPropertyChanged
	{
		private List<IPrintJob> _printJobs;
		private String _connectionString;
		private event PropertyChangedEventHandler _propertyChanged;
		private string _regularLabelLocation;
		private string _regularSmallLabelLocation;
		private string _specialSmallLabelLocation;
		private string _superSaleLabelLocation;
		private string _smartBuyLabelLocation;
		private string _smartBuySmallLabelLocation;
		private string _discontinuedLabelLocation;
		private string _discontinuedSmallLabelLocation;
		private string _nowEvenLowerLabelLocation;
		private string _1upXSign;
		private string _1upLSign;
		private string _1upMSign;
		private string _1upSSign;
		private string _2upSign;
		private string _4upSign;
		private string _SB1upXSign;
		private string _SB1upLSign;
		private string _SB1upMSign;
		private string _SB1upSSign;
		private string _SB2upSign;
		private string _SB4upSign;
        private string _discoSmallFileLocation;

		private string _xmlFileLocation;

		public List<IPrintJob> PrintJobs
		{
			get { return _printJobs; }
			set
			{
				if (_printJobs != value)
				{
					_printJobs = value;
					if (_propertyChanged != null)
						_propertyChanged(this, new PropertyChangedEventArgs("PrintJobs"));
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged
		{
			add { _propertyChanged += value; }
			remove { _propertyChanged -= value; }
		}

		public PrintManagerModel(
            ApplicationConfig appConfig, 
			string regularLabelLocation, 
			string regularSmallLabelLocation,
			string specialSmallLabelLocation,
			string superSaleLabelLocation, 
			string smartBuyLabelLocation,
			string smartBuySmallLabelLocation,
			string discontinuedLabelLocation,
			string discontinuedSmallLabelLocation,
			string nowEvenLowerLabelLocation,
			string oneUpXSign,
			string oneUpLSign,
			string oneUpMSign,
			string oneUpSSign,
			string twoUpSign,
			string fourUpSign,
			string SBoneUpXSign,
			string SBoneUpLSign,
			string SBoneUpMSign,
			string SBoneUpSSign,
			string SBtwoUpSign,
			string SBfourUpSign,
            string discoSmallFileLocation)
		{
			_regularLabelLocation = regularLabelLocation;
			_regularSmallLabelLocation = regularSmallLabelLocation;
			_specialSmallLabelLocation = specialSmallLabelLocation;
			_superSaleLabelLocation = superSaleLabelLocation;
			_smartBuyLabelLocation = smartBuyLabelLocation;
			_smartBuySmallLabelLocation = smartBuySmallLabelLocation;
			_discontinuedLabelLocation = discontinuedLabelLocation;
			_discontinuedSmallLabelLocation = discontinuedSmallLabelLocation;
			_nowEvenLowerLabelLocation = nowEvenLowerLabelLocation;
			_1upXSign = oneUpXSign;
			_1upLSign = oneUpLSign;
			_1upMSign = oneUpMSign;
			_1upSSign = oneUpSSign;
			_2upSign = twoUpSign;
			_4upSign = fourUpSign; 
			_SB1upXSign = SBoneUpXSign;
			_SB1upLSign = SBoneUpLSign;
			_SB1upMSign = SBoneUpMSign;
			_SB1upSSign = SBoneUpSSign;
			_SB2upSign = SBtwoUpSign;
			_SB4upSign = SBfourUpSign;
            _discoSmallFileLocation = discoSmallFileLocation;
			_xmlFileLocation = appConfig.PrintXml;
			_printJobs = new List<IPrintJob>();
			_connectionString = appConfig.LocalConnectionString;
			UpdateSignList();
		}

		public void UpdateSignList()
		{
			List<PrintJob> printjobs = PrintJob.GetAll(_connectionString);
			List<IPrintJob> jobs = new List<IPrintJob>();
			foreach (PrintJob job in printjobs)
			{
				jobs.Add(job);
			}

			PrintJobs = jobs;
		}

		public void RemoveJobs(List<string> list)
		{
			foreach (String jobName in list)
			{
                PrintManagerDisplay.DeleteWithJobName(_connectionString, jobName);

                //List<PrintManagerDisplay> displays = PrintManagerDisplay.GetAll(_connectionString, jobName);
                //foreach (PrintManagerDisplay display in displays)
                //{
                //    display.Delete();
                //}
			}

			UpdateSignList();
		}

		internal void Print(List<string> list)
		{
			List<PrintJob> reg18up = new List<PrintJob>();
			List<PrintJob> reg32up = new List<PrintJob>();
			List<PrintJob> ss6up = new List<PrintJob>();
			List<PrintJob> ss10up = new List<PrintJob>();
			List<PrintJob> sb6up = new List<PrintJob>();
			List<PrintJob> sb10up = new List<PrintJob>();
			List<PrintJob> sd6up = new List<PrintJob>();
			List<PrintJob> sd10up = new List<PrintJob>();
			List<PrintJob> bb6up = new List<PrintJob>();
			List<PrintJob> bb10up = new List<PrintJob>();
			List<PrintJob> regd18up = new List<PrintJob>();
			List<PrintJob> regd32up = new List<PrintJob>();
			List<PrintJob> nel6up = new List<PrintJob>();
			List<PrintJob> nel10up = new List<PrintJob>();
			List<PrintJob> ss1upx = new List<PrintJob>();
			List<PrintJob> ss1upl = new List<PrintJob>();
			List<PrintJob> ss1upm = new List<PrintJob>();
			List<PrintJob> ss1ups = new List<PrintJob>();
			List<PrintJob> ss2up = new List<PrintJob>();
			List<PrintJob> ss4up = new List<PrintJob>();
			List<PrintJob> sd1upx = new List<PrintJob>();
			List<PrintJob> sd1upl = new List<PrintJob>();
			List<PrintJob> sd1upm = new List<PrintJob>();
			List<PrintJob> sd1ups = new List<PrintJob>();
			List<PrintJob> sd2up = new List<PrintJob>();
			List<PrintJob> sd4up = new List<PrintJob>();
			List<PrintJob> sb1upx = new List<PrintJob>();
			List<PrintJob> sb1upl = new List<PrintJob>();
			List<PrintJob> sb1upm = new List<PrintJob>();
			List<PrintJob> sb1ups = new List<PrintJob>();
			List<PrintJob> sb2up = new List<PrintJob>();
			List<PrintJob> sb4up = new List<PrintJob>();

			foreach (string item in list)
			{
				//PrintJob job = new PrintJob(_connectionString, item);
				List<PrintJob> jobs = PrintJob.GetAll(_connectionString, item);

				foreach (PrintJob job in jobs)
				{
					if (job.Type == "18UPREG")
						reg18up.Add(job);
					else if (job.Type == "18UPREGD")
						regd18up.Add(job);
					else if (job.Type == "32UPREG")
						reg32up.Add(job);
					else if (job.Type == "32UPREGD")
						regd32up.Add(job);
					else if (job.Type == "6UPSS")
						ss6up.Add(job);
					else if (job.Type == "12UPSS")
						ss10up.Add(job);
					else if (job.Type == "6UPSB")
						sb6up.Add(job);
					else if (job.Type == "12UPSB")
						sb10up.Add(job);
					else if (job.Type == "6UPSD" || job.Type == "6UPSDF")
						sd6up.Add(job);
					else if (job.Type == "12UPSD" || job.Type == "12UPSDF")
						sd10up.Add(job);
					else if (job.Type == "6UPBB")
						bb6up.Add(job);
					else if (job.Type == "12UPBB")
						bb10up.Add(job);
					else if (job.Type == "6UPNEL")
						nel6up.Add(job);
					else if (job.Type == "12UPNEL")
						nel10up.Add(job);
					else if (job.Type == "1UPXSS")
						ss1upx.Add(job);
					else if (job.Type == "1UPLSS")
						ss1upl.Add(job);
					else if (job.Type == "1UPMSS")
						ss1upm.Add(job);
					else if (job.Type == "1UPSSS")
						ss1ups.Add(job);
					else if (job.Type == "2UPSS")
						ss2up.Add(job);
					else if (job.Type == "4UPSS")
						ss4up.Add(job);
					else if (job.Type == "1UPXSD")
						sd1upx.Add(job);
					else if (job.Type == "1UPLSD")
						sd1upl.Add(job);
					else if (job.Type == "1UPMSD")
						sd1upm.Add(job);
					else if (job.Type == "1UPSSD")
						sd1ups.Add(job);
					else if (job.Type == "2UPSD")
						sd2up.Add(job);
					else if (job.Type == "4UPSD")
						sd4up.Add(job);
					else if (job.Type == "1UPXSB")
						sb1upx.Add(job);
					else if (job.Type == "1UPLSB")
						sb1upl.Add(job);
					else if (job.Type == "1UPMSB")
						sb1upm.Add(job);
					else if (job.Type == "1UPSSB")
						sb1ups.Add(job);
					else if (job.Type == "2UPSB")
						sb2up.Add(job);
					else if (job.Type == "4UPSB")
						sb4up.Add(job);
				}
			}

			if (reg18up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 18 up regular labels", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    RegularInfoWriter infoPrinter = new RegularInfoWriter(_connectionString, _regularLabelLocation, reg18up, _xmlFileLocation);
                    infoPrinter.GetInfo();
                    infoPrinter.Write();
                }
			}

            if (reg32up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 32 up regular labels", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    RegularInfoWriter infoPrinter = new RegularInfoWriter(_connectionString, _regularSmallLabelLocation, reg32up, _xmlFileLocation);
                    infoPrinter.GetInfo();
                    infoPrinter.Write();
                }
            }

			if (regd18up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 18 up regular labels", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    RegularInfoWriter infoPrinter = new RegularInfoWriter(_connectionString, _discontinuedLabelLocation, regd18up, _xmlFileLocation);
                    infoPrinter.GetInfo();
                    infoPrinter.Write();
                }
			}

            if (regd32up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 32 up regular labels", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    RegularInfoWriter infoPrinter = new RegularInfoWriter(_connectionString, _discontinuedSmallLabelLocation, regd32up, _xmlFileLocation);
                    infoPrinter.GetInfo();
                    infoPrinter.Write();
                }
            }

			if (ss6up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 6 up super sale labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(_connectionString, _superSaleLabelLocation, ss6up, _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
			}

			if (ss10up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 12 up super sale labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(_connectionString, _specialSmallLabelLocation, ss10up, _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
			}

            if (sb6up.Count > 0)
            {
                DialogResult result1 = MessageBox.Show("Change print stock to 6 up smart buy labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result1 == DialogResult.OK)
                {
                    RegularInfoWriter specialWriter = new RegularInfoWriter(_connectionString, _smartBuyLabelLocation, sb6up, _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();

                    DialogResult result = MessageBox.Show("Printing Finished.", "EDLP Printing...",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                    EdlpInfo.SetNewAsPrinted(_connectionString);
                }
            }

			if (sb10up.Count > 0)
			{
				DialogResult result1 = MessageBox.Show("Change print stock to 12 up smart buy labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result1 == DialogResult.OK)
                {
                    RegularInfoWriter regularWriter = new RegularInfoWriter(_connectionString, _smartBuySmallLabelLocation, sb10up, _xmlFileLocation);
                    regularWriter.GetInfo();
                    regularWriter.Write();

                    DialogResult result = MessageBox.Show("Printing Finished.", "EDLP Printing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

                    EdlpInfo.SetNewAsPrinted(_connectionString);
                }
			}

			if (sd6up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 6 up special deal labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(_connectionString, _superSaleLabelLocation, sd6up, _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
			}

			if (sd10up.Count > 0)
			{
				DialogResult result = MessageBox.Show("Change print stock to 12 up special deal labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(_connectionString, _specialSmallLabelLocation, sd10up, _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
			}

            if (nel6up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 6 up now even lower labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(
                        _connectionString,
                        _nowEvenLowerLabelLocation,
                        nel6up,
                        _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
            }

            if (nel10up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 12 up now even lower labels.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SpecialInfoWriter specialWriter = new SpecialInfoWriter(
                        _connectionString,
                        _discoSmallFileLocation,
                        nel10up,
                        _xmlFileLocation);
                    specialWriter.GetInfo();
                    specialWriter.Write();
                }
            }

            if (ss1upx.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 14 x 11 super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upXSign,
                        ss1upx,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (ss1upl.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 13 x 10 super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upLSign,
                        ss1upl,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (ss1upm.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 8.5 super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upMSign,
                        ss1upm,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (ss1ups.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 7 super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upSSign,
                        ss1ups,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (ss2up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 2 up super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _2upSign,
                        ss2up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (ss4up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 4 up super sale signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _4upSign,
                        ss4up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd1upx.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 14 x 11 special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upXSign,
                        sd1upx,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd1upl.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 13 x 10 special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upLSign,
                        sd1upl,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd1upm.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 8.5 special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upMSign,
                        sd1upm,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd1ups.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 7 special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _1upSSign,
                        sd1ups,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd2up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 2 up special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _2upSign,
                        sd2up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sd4up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 4 up special deal signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _4upSign,
                        sd4up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sb1upx.Count > 0)
            {
                DialogResult result1 = MessageBox.Show("Change print stock to 1 up 14 x 11 smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result1 == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB1upXSign,
                        sb1upx,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();

                    DialogResult result = MessageBox.Show("Was printing successful?", "Successful?",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    if (result == DialogResult.Yes)
                    {
                        EdlpInfo.SetNewAsPrinted(_connectionString);
                    }
                }
            }

            if (sb1upl.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 13 x 10 smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB1upLSign,
                        sb1upl,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sb1upm.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 8.5 smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB1upMSign,
                        sb1upm,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sb1ups.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 1 up 11 x 7 smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB1upSSign,
                        sb1ups,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sb2up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 2 up smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB2upSign,
                        sb2up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }

            if (sb4up.Count > 0)
            {
                DialogResult result = MessageBox.Show("Change print stock to 4 up smart buy signs.", "Print Label", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    SignsWriter writer = new SignsWriter(
                        _connectionString,
                        _SB4upSign,
                        sb4up,
                        _xmlFileLocation);
                    writer.GetInfo();
                    writer.Write();
                }
            }
		}

		internal void JobViewDialog(string jobName)
		{
			JobViewDialog dialog = new JobViewDialog(_connectionString, jobName);
			dialog.ShowDialog();
			UpdateSignList();
		}
	}
}